import { Node, Label, Sprite, _decorator, Button, log, Color } from 'cc';


import { BaseComponent, cat, create, RoomSeat, RoomSeatSchema, SeatStatus } from '../../../Test';


const { ccclass, property } = _decorator;

type MatchPlayerProps = RoomSeat

@ccclass('RoomPlayer')
export class RoomPlayer extends BaseComponent<MatchPlayerProps> {


    @property({ type: Sprite, tooltip: '头像' })
    avatar: Sprite

    @property({ type: Label, tooltip: '头像' })
    nickname: Label

    @property({ type: Node, tooltip: '已准备状态' })
    state_ready: Node

    @property({ type: Node, tooltip: '等待(未准备)状态' })
    state_seated: Node

    @property({ type: Node, tooltip: '游戏中状态' })
    state_playing: Node

    @property({ type: Button, tooltip: 'ban人按钮' })
    btn_ban: Button

    @property({ type: Button, tooltip: '交换座位按钮' })
    btn_swap_seat: Button

    private lastSwapTimes: { [key: string]: number } = {};

    override props: RoomSeat = create(RoomSeatSchema)

    protected override initUI(): void {
        this.avatar.node.active = this.state_ready.active = this.state_seated.active = this.btn_ban.node.active = this.btn_swap_seat.node.active = false;

        // this.bg.node.on(Node.EventType.TOUCH_END, this.onSwapSeatHandler, this)
        this.btn_ban.node.on(Button.EventType.CLICK, this.onBanHandler, this)
        this.btn_swap_seat.node.on(Button.EventType.CLICK, this.onSwapSeatHandler, this)
    }

    protected override onAutoObserver(): void {
        this.addAutorun([
            () => {
                this.state_seated.active = this.state_ready.active = this.state_playing.active = false
                switch (this.props.status) {
                    case SeatStatus.SEATED:
                        this.state_seated.active = true
                        break;
                    case SeatStatus.READY:
                        this.state_ready.active = true
                        break;
                    case SeatStatus.PLAYING:
                        this.state_playing.active = true
                        break;
                    default:
                        break;
                }
            },
            () => {
                if (this.props.user) {
                    this.nickname.string = cat.util.stringUtil.sub(this.props.user.nickname, 12);
                    this.nickname.node.active = true
                } else {
                    this.nickname.node.active = false
                }
            },
            () => {
                this.nickname.color = this.props.user?.uid === cat.store.user.userId ? new Color().fromHEX("FFE400") : new Color().fromHEX("FFFFFF");
            },
            // () => {
            //     this.btn_swap_seat.node.active = cat.store.user.userId !== this.props.user?.uid
            // },
            () => {
                if (this.props.user?.avatar) {
                    this.avatar.node.active = true
                    cat.util.nodeUtils.setSpriteFrameToSprite(this.props.user?.avatar, this.avatar, false)
                } else {
                    this.avatar.node.active = false
                }
            }
        ])
    }

    protected override start(): void {
        // const nonce = store.room.room.mode === BattleMode.PVP_SOLO ? this.props.teamOffset : 0
        // this.bg.spriteFrame = this.bg_sprite_frames[nonce];
    }

    // @AudioEffect()
    private onBanHandler() {

    }

    // @AudioEffect()
    private onSwapSeatHandler() {

        if (cat.store.room.userSeat?.offset === this.props.offset) return

        log('换位', cat.store.room.roomInfo.id, this.props.offset)
        cat.room_ws.RoomSeatSwap(cat.store.room.roomInfo.id, this.props.offset)
    }
}
